package com.be.beadmin.quality.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.quality.dto.input.ProductQualityDto;
import com.be.beadmin.quality.dto.output.InspectionStandardEx;
import com.be.beadmin.quality.dto.output.ProductQualityEx;
import com.be.beadmin.quality.dto.output.ProductQualityInspectionByPage;
import com.be.beadmin.quality.service.ProductQualityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author zjm
* @date 2023-05-16
**/
@Api(tags = {"柜体入厂检验"})
@RestController
public class ProductQualityController {

    @Autowired
    private  ProductQualityService productQualityService;

    @ApiOperation("根据入厂检验id获取（入厂检验+检验单模板+检验单填值），综合信息")
    @PostMapping(value = "/quality/getProductQualityInspectionAllInfoByProductQualityId")
    public ResultModel getProductQualityInspectionAllInfoByProductQualityId(
            @ApiParam(value = "productQualityId",required = true)@RequestParam String productQualityId)  {

        return ResultModel.success(productQualityService.getProductQualityInspectionAllInfoByProductQualityId(productQualityId));
    }




    @ApiOperation("获取检验标准")
    @PostMapping(value = "/quality/getInspectionStandard")
    public ResultModel getInspectionStandard()  {
        List<InspectionStandardEx> inspectionStandardExList = productQualityService.getInspectionStandard();
        return ResultModel.success(inspectionStandardExList) ;
    }

    @ApiOperation("获取项目下的检验团队成员")
    @PostMapping(value = "/quality/getQualityTeamMembers")
    public ResultModel getQualityTeamMembers(
            @ApiParam(value = "projectId") @RequestParam String projectId
    )  {
        try {
            return ResultModel.success(productQualityService.getQualityTeamMembers(projectId)) ;
        } catch (BeadminException e) {
            return ResultModel.error(-1,e.getMessage());
        } catch (Exception e) {
            return ResultModel.error(-2,e.getMessage());
        }
    }

    @ApiOperation("某个柜子的所有检验记录（详情tab)")
    @PostMapping(value = "/quality/getProductQualityInspectionByPage")
    public ResultModel getProductQualityInspectionByPage(@RequestBody ProductQualityInspectionByPage pageEntity)  {
        return ResultModel.success(productQualityService.getProductQualityInspectionByPage(pageEntity)) ;
    }

    @PostMapping(value = "/quality/getProductQualityByPage")
    public ResultModel getProductQualityByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(productQualityService.getProductQualityByPage(pageEntity)) ;
    }

    @PostMapping("/quality/addProductQuality")
    public ResultModel addProductQuality(@RequestBody ProductQualityDto productQualityDto){
        productQualityService.addProductQuality(productQualityDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/quality/delProductQuality")
    public ResultModel delProductQuality(@RequestBody ProductQualityDto productQualityDto){
        productQualityService.delProductQuality(productQualityDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }

    @ApiOperation("检验提交（0未检验，1检验合格，-1检验不合格，2 让步接收）")
    @PostMapping("/quality/updateProductQuality")
    public ResultModel updateProductQuality(@RequestBody ProductQualityDto productQualityDto){
        productQualityService.updateProductQuality(productQualityDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/quality/exportProductQuality")
    public void exportProductQuality(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<ProductQualityEx> productQualityPage = productQualityService.getProductQualityByPage(pageEntity);
        List<ProductQualityEx> productQualityExes = productQualityPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("ProductQuality接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ProductQualityEx.class).sheet("sheet1").doWrite(productQualityExes);
    }
}